package com.us.usws.service.ro.impl;

import java.util.List;

import net.iwsh.core.security.digest.DigestUtils;

import com.us.usws.cache.BaseDictCache;
import com.us.usws.common.SiteConstants;
import com.us.usws.dao.UserDAO;
import com.us.usws.model.domain.base.BaseDict;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.ro.UserROService;
import com.us.usws.support.ext.CacheManager;
import com.us.usws.support.uitls.DataPageUtil;

public class UserROServiceImpl implements UserROService {

	/*
	 * dao instance
	 */
	private UserDAO dao = new UserDAO();

	/**
	 * get user by loginName and passwd
	 */
	@Override
	public User getUserByLogin(String loginName, String passwd) {
		String md5Passwd = DigestUtils.md5Salt(passwd);
		return dao.getUserByLoginNameMD5Passwd(loginName, md5Passwd);
	}

	@Override
	public User getUser(String uid) {
		return dao.getUser(uid);
	}

	@Override
	public DOUser getDOUser(String uid) {
		return dao.getDOUser(uid);
	}

	@Override
	public User getUserByLoginName(String loginName) {
		return dao.getUserByLoginName(loginName);
	}

	@Override
	public DOUser getDOUserByLogin(String loginName, String passwd) {
		String md5Passwd = DigestUtils.md5Salt(passwd);
		return dao.getDOUserByLogin(loginName, md5Passwd);
	}

	@Override
	public DOUser getDOUserByLoginName(String loginName) {
		return dao.getDOUserByLoginName(loginName);
	}

	@Override
	public List<User> getUsersByDept(String deptUid) {
		return dao.getUsersByDept(deptUid);
	}

	public int getUsersTotalCount() {
		return dao.getUsersTotalCount();
	}

	public void loadUsersByPaging(DataPageUtil<User> dataPageUtil) {
		dao.loadUsersByPaging(dataPageUtil);
		BaseDictCache dictCache = CacheManager.getDictCache();
		List<User> users = dataPageUtil.getResult();
		if(users != null && users.size() > 0) {
			for(User user:users) {
				BaseDict bd = dictCache.getBaseDictBySepId(SiteConstants.DICT_DOMAIN_TARGET_ROLE, user.getRole());
				if(bd != null) {
					user.setRoleName(bd.getName());
				}
			}
		}
	}

	@Override
	public List<User> getAllValidUsers() {
		return dao.getAllValidUsers();
	}

}
